{{--选择属性--}}
<script>
    // 定义一个全局变量用来存货品的id
    var productId = {{$product['id']}};
    var attributes = {!! json_encode($product['attributeArr']) !!};
    console.log(productId);
    $('.product-options li.radio').click(function () {
        $(this).addClass('active').siblings('li.radio').removeClass('active');
        //定义一个变量用来存放接收的id
        var attrs = [];
        $.each($('.active'), function () {
            attrs.push($(this).attr('good'));
            // console.log(attrs);
            $.ajax({
                url: '/getProduct/' + attrs + '/{{$good['id']}}',
                method: 'get',
                dataType: 'json',
                success: function (res) {
                    console.log(res);
                    if (!res || res.inventory == 0) {
                        $('#button-cart').val('暂时缺货');
                        $('.inventory').html('0件')
                    } else {
                        $('#button-cart').val('加入购物车');
                        $('.inventory').html(res.inventory + '件');
                    }
                    // 获取货品的id
                    if (res.id) {
                        var productId = res.id;
                        var attributes = res.attributeArr;
                    }
                }
            })
        })


    })

</script>


{{--加入购物车--}}

<script>
    function cartAdd() {
        //获取商品的id
        var good_id = {{$good['id']}};
        var product_id = productId;
        console.log(product_id);
        var number = $('.inventorys').val();
        //属性组合
        var attribute = attributes;
        location.href = '/cart/cart/addToCart/' + good_id + '/' + product_id + '/' + number+'/'+attribute;
    }


</script>




